package com.example.java2e.Controller;

import com.example.java2e.Entity.Vehicle;
import com.example.java2e.Service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;
    @PostMapping("/add_vehicle")
    public Map<String, Object> add_vehicle(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try{
            String id = (String) front_map.get("id");
            String type = (String) front_map.get("type");
            String status = (String) front_map.get("status");
            Double mileage = Double.parseDouble((String) front_map.get("mileage")) ;
            String check_time = (String) front_map.get("check_time");
            Long capacity = Long.parseLong((String) front_map.get("capacity"));
            if(vehicleService.checkVehicleInfoDetail(id)!=null){
                back_map.put("code","车辆已存在!");
            }
            else if(mileage<0){
                back_map.put("code","里程数应为正!");
            }
            else{
                vehicleService.addVehicle(id,type,status,mileage,check_time,capacity);
                back_map.put("code",'1');
            }

        }catch (Exception e){
            back_map.put("code",'0');
        }
        return back_map;
    }
    @PostMapping("/change_vehicle_info")
    public Map<String, Object> change_vehicle_info(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try{
            String id = front_map.get("id").toString();
            String type = front_map.get("type").toString();
            String status = front_map.get("status").toString();
            Double mileage = Double.parseDouble(front_map.get("mileage").toString()) ;
            String check_time = front_map.get("check_time").toString();
            Long capacity = Long.parseLong(front_map.get("capacity").toString());
            if(vehicleService.checkVehicleInfoDetail(id)==null){
                back_map.put("code","车辆不存在!");
            }
            else if(mileage<0){
                back_map.put("code","里程数应为正!");
            }
            else{
                vehicleService.changeVehicleInfo(id,type,status,mileage,check_time,capacity);
                back_map.put("code",'1');
                List<Vehicle> allVehicle = vehicleService.checkVehicleInfo();
                for (Vehicle vehicle : allVehicle ){
                    String[] array = new String[]{vehicle.getId(), vehicle.getType()};
                    back_map.put(vehicle.getId(),array);
                }
            }
        }catch (Exception e){
            back_map.put("code", e.toString());
        }
        return back_map;
    }
    @PostMapping("/check_vehicle_info")
    public Map<String, Object> check_vehicle_info(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        List<Vehicle> allVehicle = vehicleService.checkVehicleInfo();
        for (Vehicle vehicle : allVehicle ){
            String[] array = new String[]{vehicle.getId(), vehicle.getType()};
            back_map.put(vehicle.getId(),array);
        }
        return back_map;
    }
    @PostMapping("/check_vehicle_info_detail")
    public Map<String, Object> check_vehicle_info_detail(@RequestBody Map<String, Object> front_map){
        String id = (String) front_map.get("id");
        Map<String, Object> back_map = new HashMap<>();
        Vehicle vehicle = vehicleService.checkVehicleInfoDetail(id);
        if(vehicle!=null){
            back_map.put("id",vehicle.getId());
            back_map.put("type",vehicle.getType());
            back_map.put("status",vehicle.getStatus());
            back_map.put("mileage",vehicle.getMileage());
            back_map.put("check_time",vehicle.getCheck_time());
            back_map.put("capacity",vehicle.getCapacity());
        }
        else back_map.put("code","车辆不存在！");
        return back_map;
    }
    @PostMapping("/delete_vehicle")
    public Map<String, Object> delete_vehicle(@RequestBody Map<String, Object> front_map){
        System.out.println("got delete request");
        String id = front_map.get("id").toString();
        System.out.println(id);
        Map<String, Object> back_map = new HashMap<>();
        vehicleService.deleteVehicle(id);
        List<Vehicle> allVehicle = vehicleService.checkVehicleInfo();
        for (Vehicle vehicle : allVehicle ){
            String[] array = new String[]{vehicle.getId(), vehicle.getType()};
            back_map.put(vehicle.getId(),array);
        }
        return back_map;
    }
}
